// types/request.d.ts
import { Ref } from 'vue';

export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'HEAD' | 'OPTIONS';

export interface RequestOptions {
    method?: HttpMethod;
    headers?: Record<string, string>;
    data?: any;
    params?: Record<string, any>;
    timeout?: number;
    showLoading?: boolean;
    loadingText?: string;
    showError?: boolean;
    withToken?: boolean;
    retryCount?: number;
    retryInterval?: number;
    cacheTime?: number;
    throttle?: boolean;
    throttleTime?: number;
    debounce?: boolean;
    debounceTime?: number;
    pollingInterval?: number;
    pollingMaxCount?: number;
    manual?: boolean;
    ready?: boolean | Ref<boolean>;
    priority?: number;
    requestId?: string;
}

export interface RequestResult<T = any> {
    data: Ref<T | undefined>;
    loading: Ref<boolean>;
    error: Ref<Error | null>;
    polling: Ref<boolean>;
    run: (params?: any) => Promise<T>;
    cancel: () => void;
    refresh: () => Promise<T>;
    clearCache: () => void;
    startPolling: () => void;
    stopPolling: () => void;
    updateParams: (params: any) => void;
    updateOptions: (options: Partial<RequestOptions>) => void;
    reset: () => void;
}

export interface CacheData<T = any> {
    data: T;
    timestamp: number;
    params: any;
}

export interface RequestTask {
    abort: () => void;
}

export interface RequestPlugin {
    onInit?: (options: RequestOptions) => RequestOptions;
    onRequest?: (options: RequestOptions) => RequestOptions | Promise<RequestOptions>;
    onSuccess?: <T>(data: T, options: RequestOptions) => T | Promise<T>;
    onError?: (error: Error, options: RequestOptions) => Error | Promise<Error> | void;
    onFinally?: (options: RequestOptions) => void | Promise<void>;
}
